﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
namespace WEALTH
{
/// <summary>
/// BasePage 的摘要说明
/// </summary>
public class BasePage : Page
{
    protected string userid = "";
    protected string usernm = "";
    protected string userrole = "";
    /// <summary>
    /// 页面显示数据最大条数
    /// </summary>
    public int pageSize = Convert.ToInt32(ConfigurationManager.AppSettings["PageSize"]);
    /// <summary>
    /// 当前页号
    /// </summary>
    public int pageIndex = 0;
    /// <summary>
    /// 总页数
    /// </summary>
    public int pageCount =0;
    /// <summary>
    /// 总数据条数
    /// </summary>
    public int recordCount = 0;

    protected override void OnInit(EventArgs e)
    {
        if (Session["UserName"] == null || string.IsNullOrEmpty(Session["UserName"].ToString()))
        {
            ClientScript.RegisterStartupScript(GetType(), "", "<script>Index.Login()</script>");
            return;
        }
        if (Session["msg"] != null)
        {
            ShowMsg(Session["msg"].ToString(), 0);
            Session["msg"] = null;
        }
        base.OnInit(e);
        
    }
    public string UserName
    {
        get
        {
            return (string)HttpContext.Current.Session["User_ID"];
        }
        set
        {
            if (value == null)
            {
                HttpContext.Current.Session.Remove("User_ID");
            }
            else
            {
                HttpContext.Current.Session["User_ID"] = value;
            }
        }
    }

    /// <summary>
    /// 弹出消息框方法
    /// </summary>
    /// <param name="msg">显示信息</param>
    /// <param name="type">类型： -1，一般消息框； 0，执行成功消息框； 1，执行失败消息框</param>
    protected void ShowMsg(string msg, int type)
    {
        if (type == -1)
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>jBox.tip('" + SpecialCharHandler.toJSString(msg) + "', '')</script>");
        }
        else if (type == 0)
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>jBox.tip('" + SpecialCharHandler.toJSString(msg) + "', 'success')</script>");
        }
        else if (type == 1)
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>jBox.tip('" + SpecialCharHandler.toJSString(msg) + "', 'error')</script>");
        }

    }

    protected void ShowMsg(string msg, string key)
    {
        if (!ClientScript.IsStartupScriptRegistered(key))
        {
            ClientScript.RegisterStartupScript(GetType(), key, "alert('" + msg + "')", true);
        }
    }
    /// <summary>
    /// 执行成功后跳转到指定页面
    /// </summary>
    /// <param name="weburl">指定页面URL 例子： UserList.aspx</param>
    /// <param name="msg">需要传提示信息参数 例子：添加用户成功！</param>
    protected void ReturnList(string weburl, string msg)
    {
        if (!string.IsNullOrEmpty(msg))
        {
            Response.Write("<script>top.$('#loading').show();location.replace('" + weburl + "');</script>");
            Session["msg"] = msg;
        }
    }

    /// <summary>
    /// setSortColumnStyle
    /// </summary>
    /// <param name="gv"></param>
    /// <param name="sortExpression"></param>
    /// <returns></returns>
    protected void setSortColumnStyle(GridView gv, string sortExpression, string sortDirection)
    {
        for (int i = 0; i < gv.Columns.Count; i++)
        {
            if (sortExpression.Equals(gv.Columns[i].SortExpression))
            {
                string currentClass = gv.Columns[i].HeaderStyle.CssClass;
                currentClass = currentClass.Replace(" asc ", "");
                currentClass = currentClass.Replace(" desc ", "");
                gv.Columns[i].HeaderStyle.CssClass = currentClass + " " + sortDirection.ToLower() + " ";
            }
            else
            {
                gv.Columns[i].HeaderStyle.Reset();
            }
        }
    }

    /// <summary>
    /// 获取当前用户
    /// </summary>
    /// <returns>用户Pojo</returns>
    protected UserCurrent getCurrentUser()
    {
        if (Session[SessionKeys.CURRENT_USER] == null)
        {
            ClientScript.RegisterStartupScript(GetType(), "", "<script>Index.Login()</script>");
            return new UserCurrent();
        }
        return (UserCurrent)Session[SessionKeys.CURRENT_USER];
    }
}
}